﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WorkStation.Frameworks.Core;
using WorkStation.Frameworks.Core.Extensions;
using WorkStation.Frameworks.EasyBus.Pipeline.Filters;
using WorkStation.Frameworks.EasyBus.Pipeline.Handles;
using WorkStation.Frameworks.EasyBus.Pipeline.Modules;

namespace WorkStation.Frameworks.EasyBus.Pipeline.Extensions
{
    /// <summary>
    /// 管道扩展
    /// </summary>
    public static class IPipelineExtensions
    {
        private static readonly object LOCK_OBJ = new object();

        /// <summary>
        /// 订阅
        /// </summary>
        /// <typeparam name="TInput">输入对象类型</typeparam>
        /// <param name="pipeline">管道对象</param>
        /// <param name="handle">处理器对象</param>
        /// <returns></returns>
        public static IPipeline Subscribe<TInput>(this IPipeline pipeline, IHandle handle)
        {
            string inputType = typeof(TInput).FullName;

            pipeline.Subscribe(inputType, handle);

            return pipeline;
        }

        /// <summary>
        /// 订阅
        /// </summary>
        /// <typeparam name="TInput">输入对象类型</typeparam>
        /// <param name="pipeline">管道对象</param>
        /// <param name="func">处理内容委托</param>
        /// <param name="handleName">处理器名称</param>
        /// <param name="filter">过滤器</param>
        /// <param name="order">顺序</param>
        /// <returns></returns>
        public static IPipeline Subscribe<TInput>(this IPipeline pipeline, Func<ActionContext, IHandle, HandleResult> func, string handleName = null, IFilter filter = null, int order = -1)
        {
            pipeline.Subscribe<TInput>(new Handle(handleName, filter, order).RegisterHandle(func));

            return pipeline;
        }

        /// <summary>
        /// 异步订阅
        /// </summary>
        /// <typeparam name="TInput">输入对象类型</typeparam>
        /// <param name="pipeline">管道对象</param>
        /// <param name="func">处理内容委托</param>
        /// <param name="handleName">处理器名称</param>
        /// <param name="filter">过滤器</param>
        /// <returns></returns>
        public static IPipeline AsyncSubscribe<TInput>(this IPipeline pipeline, Func<ActionContext, IHandle, HandleResult> func, string handleName = null, IFilter filter = null)
        {
            pipeline.Subscribe<TInput>(new AsyncHandle(handleName, filter).RegisterHandle(func));

            return pipeline;
        }

        /// <summary>
        /// 订阅
        /// </summary>
        /// <param name="pipeline">管道对象</param>
        /// <param name="inputType">输入类型全名</param>
        /// <param name="handle">处理器</param>
        /// <returns></returns>
        public static IPipeline Subscribe(this IPipeline pipeline, string inputType, IHandle handle)
        {
            IModule module = null;

            lock (LOCK_OBJ)
            {
                if (!pipeline.Modules.TryGetValue(inputType, out module))
                {
                    // 注册System.Object类型为全局处理
                    pipeline.Modules.Add(inputType, new Module(inputType) { Order = inputType.Equals(typeof(object).FullName) ? -100 : 0 });
                }
            }

            pipeline.Modules[inputType].Subscribe(handle);

            return pipeline;
        }

        /// <summary>
        /// 订阅
        /// </summary>
        /// <param name="pipeline">管道对象</param>
        /// <param name="inputType">输入类型全名</param>
        /// <param name="func">处理内容委托</param>
        /// <param name="handleName">处理器名称</param>
        /// <param name="filter">过滤器</param>
        /// <param name="order">顺序</param>
        /// <returns></returns>
        public static IPipeline Subscribe(this IPipeline pipeline, string inputType, Func<ActionContext, IHandle, HandleResult> func, string handleName = null, IFilter filter = null, int order = -1) 
        {
            pipeline.Subscribe(inputType, new Handle(handleName, filter, order).RegisterHandle(func));

            return pipeline;
        }

        /// <summary>
        /// 退订
        /// </summary>
        /// <param name="pipeline">管道对象</param>
        /// <param name="inputType">输入类型</param>
        /// <param name="handleName">处理器名称</param>
        /// <returns></returns>
        public static IPipeline UnSubscribe(this IPipeline pipeline, string inputType, string handleName) 
        {
            lock (LOCK_OBJ) 
            {
                IModule module = null;

                if (pipeline.Modules.TryGetValue(inputType, out module)) 
                {
                    module.Handles.Remove(handleName);
                }
            }

            return pipeline;
        }

        /// <summary>
        /// 获取模块
        /// </summary>
        /// <param name="pipeline">对象管道</param>
        /// <param name="msgType">消息类型</param>
        /// <returns></returns>
        public static IModule GetModule(this IPipeline pipeline, string msgType) 
        {
            return pipeline.Modules.FirstOrDefault(v => v.Key.Equals(msgType)).Change(v => v.Value);
        }

        /// <summary>
        /// 获取处理器
        /// </summary>
        /// <param name="pipeline">对象管道</param>
        /// <param name="routeKey">路由关键字</param>
        /// <param name="handleName">处理器名称</param>
        /// <returns></returns>
        public static IHandle GetHandle(this IPipeline pipeline, string routeKey, string handleName) 
        {
            return pipeline.GetModule(routeKey)
                           .Change(v => 
                           { 
                               IHandle handle = null; 
                               return v.Handles.TryGetValue(handleName, out handle).WhenTrue(() => handle); 
                           });
        }
    }
}
